package fr.pud.client.controller;

import fr.pud.client.controller.address.AddAddressTODELETE;
import fr.pud.client.controller.address.DisplayOrHideAddressesSuggestions;
import fr.pud.client.controller.address.GotoListener;
import fr.pud.client.controller.address.LoadAddressesFile;
import fr.pud.client.controller.bubble.CloseBubble;
import fr.pud.client.controller.fleet.LoadFleet;
import fr.pud.client.controller.fleet.OpenFrameAddVehicle;
import fr.pud.client.controller.fleet.SaveFleet;
import fr.pud.client.controller.query.DisplayOrHideQueries;
import fr.pud.client.controller.query.LoadQueries;
import fr.pud.client.controller.query.OpenQueryFrame;
import fr.pud.client.controller.query.SaveQueries;
import fr.pud.client.controller.routes.DisplayOrHideMapRoutes;
import fr.pud.client.controller.routes.LoadRoutes;
import fr.pud.client.controller.routes.OpenFrameHideOrDisplayRoute;
import fr.pud.client.controller.routes.SaveRoutes;
import fr.pud.client.view.JMapViewerExtended;
import fr.pud.client.view.MainFrame;
import fr.pud.client.view.mapstation.Bubble;
import fr.pud.utils.Tools;

/**
 * Controller class = Initialization of all listeners, observers and
 * AbstractAction
 * Apr 9, 2012 - OSE.
 * @author Simon Devineau, Adrien Hierry, Leopold Houdin, Pierre Reliquet
 *         Ecole des Mines de Nantes
 *         Controller.java
 */
public class Controller {
    /**
     * A short link to the main frame.
     */
    private static MainFrame frame;

    /**
     * The default constructor where all the listeners are added
     */
    public Controller() {
        frame = MainFrame.getMainFrame();
        OpenResultFile actionOpenResultFile = new OpenResultFile();
        LoadAddressesFile loadAddressesFile = new LoadAddressesFile();
        OpenFrameAddVehicle addAddVehicleToFleet = new OpenFrameAddVehicle();
        OpenQueryFrame openQueryFrame = new OpenQueryFrame();
        SaveFleet saveFleet = new SaveFleet();
        LoadFleet loadFleet = new LoadFleet();
        SaveQueries saveQueries = new SaveQueries();
        LoadQueries loadQueries = new LoadQueries();
        LoadRoutes loadRoutes = new LoadRoutes();
        SaveRoutes saveRoutes = new SaveRoutes();
        /*
         * Menu
         */
        frame.getPrintXMLFile().setAction(actionOpenResultFile);
        frame.getAddVehicle().setAction(addAddVehicleToFleet);
        frame.getMenuItemCreateQuery().setAction(openQueryFrame);
        frame.getSaveFleet().setAction(saveFleet);
        frame.getLoadFleet().setAction(loadFleet);
        frame.getSaveQueries().setAction(saveQueries);
        frame.getLoadQueries().setAction(loadQueries);
        frame.getLoadAddressFiler().setAction(loadAddressesFile);
        frame.getLoadRoutes().setAction(loadRoutes);
        frame.getSaveRoutes().setAction(saveRoutes);
        frame.getChangeVisibilityQueries()
                .setAction(new DisplayOrHideQueries());
        frame.getChangeVisibilityAddressesSuggestions().setAction(
                new DisplayOrHideAddressesSuggestions());
        frame.getChangeVisibilityMapRoutes().setAction(
                new DisplayOrHideMapRoutes());
        frame.getButtonHideOrDisplayRoute().setAction(
                new OpenFrameHideOrDisplayRoute());
        /*
         * Toolbar
         */
        GotoListener goToListener = new GotoListener();
        frame.getGoToButton().setAction(goToListener);
        frame.getGoToText().addActionListener(goToListener);
        frame.getAddVehicleToolBar().setAction(addAddVehicleToFleet);
        frame.getCreateQuery().setAction(openQueryFrame);
        frame.getLoadAddressFileToolbar().setAction(loadAddressesFile);
        Bubble.getButtonClose().setAction(new CloseBubble());
        frame.getPrintXMLFileToolBar().setAction(actionOpenResultFile);
        CloseProgram close = new CloseProgram();
        frame.getCloseSoftwareToolBar().setAction(close);
        frame.getQuit().setAction(close);
        LaunchComputation compute = new LaunchComputation();
        frame.getLaunchComputation().setAction(compute);
        JMapViewerExtended.getJMapViewer().getContextMenuItem()
                .addMouseListener(new AddAddressTODELETE());
    }
}
